package org.zxp.thread.ccontainer;

import java.util.concurrent.SynchronousQueue;

/**
 * @program: zxp-thread-test
 * @description: 测试SynchronousQueue
 * 单独调用take put 都会阻塞
 * 比如调用take阻塞了
 * 这时另一个线程再调用一个put，put和take都会成功解除阻塞，并且take会获得put的这个值
 * @author: X-Pacific zhang
 * @create: 2019-05-30 15:23
 **/
public class SynchronousQueueTest {
    public static void main(String[] args) throws InterruptedException {
        SynchronousQueue<String> queue = new SynchronousQueue();
        new Thread(() -> {
            try {
                System.out.println("take-before");
                queue.put("zxp");
                System.out.println("take-after");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        Thread.sleep(2000L);
        System.out.println("put-before");
        String msg = queue.take();
        System.out.println("put-after =" + msg);
        for (int i = 0; i < 10; i++) {
            //全是false
            System.out.println(queue .offer("1"));
            //全是null
            System.out.println(queue .poll());
        }
    }
}
